<?php


namespace App\Http\Controllers\Admin;

use App\DTOS\Admin\UserMemberDTO;
use App\Exports\ExcelExport;
use App\Http\Controllers\Controller;
use App\Models\Game\Map;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\ResponseUtil;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ChangeSceneController extends Controller
{
    private Request $request;
    private UserRoleService $userRoleService;
    private UserMemberService $userMemberService;

    /**
     * ChangeSceneController constructor.
     * @param Request $request
     * @param UserRoleService $userRoleService
     * @param UserMemberService $userMemberService
     */
    public function __construct(Request $request, UserRoleService $userRoleService, UserMemberService $userMemberService)
    {
        $this->request = $request;
        $this->userRoleService = $userRoleService;
        $this->userMemberService = $userMemberService;
    }

    public function paginate(){
        $searchData = $this->request->all();
        $account = data_get($searchData,'account');
        $sceneName = data_get($searchData,'sceneName');
        if ($account){
            $userMemberDTO = new UserMemberDTO(['username'=>$account]);
            $userMemberDTO->setFields(['id']);
            $paginate = $this->userMemberService->paginate($userMemberDTO);
            $searchMemIds = collect($paginate->items())->pluck('id')->toArray();
            if (empty($searchMemIds)){
                return ResponseUtil::error("无数据");
            }
            $searchData['memIds'] = $searchMemIds;
        }
        if ($sceneName){
            $sceneIds = Map::query()->where("name",'like',"%$sceneName%")->pluck('id')->toArray();
            if (empty($sceneIds)){
                return ResponseUtil::error("无数据");
            }
            $searchData['sceneIds'] = $sceneIds;
        }
        global $gameArea;
        $result = callRpc("/ChangeSceneService/paginate",$searchData,['gameArea'=>$gameArea]);
        $memIds = collect($result['data'])->pluck('memId')->unique()->toArray();
        $members = $this->userMemberService->getByIds($memIds);
        foreach($result['data'] as &$datum){
            $datum['member'] = collect($members)->where('id',$datum['memId'])->first();

        }
        return ResponseUtil::json($result);
    }

    public function export(){

        $searchData = $this->request->all();
        $account = data_get($searchData,'account');
        $sceneName = data_get($searchData,'sceneName');
        if ($account){
            $userMemberDTO = new UserMemberDTO(['username'=>$account]);
            $userMemberDTO->setFields(['id']);
            $paginate = $this->userMemberService->paginate($userMemberDTO);
            $searchMemIds = collect($paginate->items())->pluck('id')->toArray();
            if (empty($searchMemIds)){
                return ResponseUtil::error("无数据");
            }
            $searchData['memIds'] = $searchMemIds;
        }
        if ($sceneName){
            $sceneIds = Map::query()->where("name",'like',"%$sceneName%")->pluck('id')->toArray();
            if (empty($sceneIds)){
                return ResponseUtil::error("无数据");
            }
            $searchData['sceneIds'] = $sceneIds;
        }
        global $gameArea;
        $result = callRpc("/ChangeSceneService/paginate",$searchData,['gameArea'=>$gameArea]);
        $memIds = collect($result['data'])->pluck('memId')->unique()->toArray();
        $members = $this->userMemberService->getByIds($memIds)->toArray();
        $excelRows = [
            ['记录时间','服务器ID','游戏账号','角色ID','角色名','角色等级','原场景','目的场景','场景类型']
        ];

        $mapList = Map::query()->get()->toArray();
        foreach($result['data'] as &$datum){
            $member = collect($members)->where('id',$datum['memId'])->first();
            $srcMap = collect($mapList)->where('id',data_get($datum,'src_id'))->first();
            $tarMap = collect($mapList)->where('id',data_get($datum,'tar_id'))->first();
            $excelRows[] = [
                date("Y-m-d H:i:s",data_get($datum,'logTime')),
                data_get($datum,'serverId'),
                data_get($member,'username'),
                data_get($datum,'roleId'),
                data_get($datum,'nick'),
                data_get($datum,'lv'),
                data_get($srcMap,'name'),
                data_get($tarMap,'name'),
                '',
            ];
        }

        return ResponseUtil::success(cacheDownload(arrayToScvContent($excelRows),'.场景日志.csv'));
    }
}
